package com.bocai.freeman.view.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 订单Frag
 */
public class OrderFrag extends BaseFragment implements RadioGroup.OnCheckedChangeListener {


    View view;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rbtn_demand)
    RadioButton rbtnDemand;
    @Bind(R.id.rbtn_kills)
    RadioButton rbtnKills;
    @Bind(R.id.rbtn_special_perf)
    RadioButton rbtnSpecialPerf;
    @Bind(R.id.rg_order_choose)
    RadioGroup rgOrderChoose;

    private FragmentTransaction ft;
    private Fragment CURRENT_FRAGMENT;
    private List<Fragment> fragments;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_order, null);
        ButterKnife.bind(this, view);
        initView();
        initEvent();
        return view;
    }

    @Override
    public void initEvent() {
        rgOrderChoose.setOnCheckedChangeListener(this);
    }

    private void initView() {
        title.setText("订单");
        fragments = new ArrayList<>();
        fragments.add(new OrderDemandFrag());
        fragments.add(new OrderSkillFrag());
        fragments.add(new OrderSpecialPerfFrag());
        goFragment(0);
    }


    public void goFragment(int index) {

        ft = getActivity().getSupportFragmentManager().beginTransaction();
        if (null != CURRENT_FRAGMENT) {
            ft.hide(CURRENT_FRAGMENT);
        }
        Fragment fragment = getActivity().getSupportFragmentManager()
                .findFragmentByTag(fragments.get(index).getClass().getName());
        if (fragment == null) {
            fragment = fragments.get(index);
        }
        CURRENT_FRAGMENT = fragment;
        if (!fragment.isAdded()) {
            ft.add(R.id.fl_order_fragment, fragment, fragment.getClass().getName());
        } else {
            ft.show(fragment);
        }
        ft.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rbtn_demand:
                goFragment(0);
                break;
            case R.id.rbtn_kills:
                goFragment(1);
                break;
            case R.id.rbtn_special_perf:
                goFragment(2);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
